﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E_Shop.Common;
using ESHOP.DAO;
using ESHOP.Domain;
namespace E_Shop
{
    public partial class CompanyProfile : Authentication
    {
        private readonly CONGTYDAO _congtydao = new CONGTYDAO();
        private readonly DanhMucDAO _danhmuc = new DanhMucDAO();
        private readonly QuocGiaDAO _quocgia = new QuocGiaDAO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Acount == null)
            {
                Response.Redirect(WebUrl.LOGIN, true);
            }
            else
            {
                if (!IsPostBack)
                {
                    Loadreference();
                }
                SetToForm();
            }
         }
        private void Loadreference()
        {
            CommonFunc.BindDropDownList(ddlRegion,_quocgia.GetList(),"TENQUOCGIA","ID", true);
            CommonFunc.BindDropDownList(ddlCategory, _danhmuc.GetList(), "TENDM", "ID", true);
          //  Upinfor.Update();
        }
        private void SetToForm()
        {
            var obj = _congtydao.Get(Acount.MACT);
            txtCompanyName.Text = obj.TENCONGTY==null ? "" : obj.TENCONGTY.ToString();
            txtwebsite.Text = obj.WEBSITE==null ? "" : obj.WEBSITE.ToString();
            txtMail.Text = obj.Email==null?"":obj.Email.ToString();
            txtmota.Text = obj.MOTA==null? "" : obj.MOTA.ToString();
            txtkeyword.Text = obj.KEYWORD==null? "" : obj.KEYWORD.ToString();
            var index = ddlCategory.Items.FindByValue(obj.MADM);
            if(index!=null)
                ddlCategory.SelectedIndex = ddlCategory.Items.IndexOf(index);
            ddlCategory.SelectedIndex = 0;
            index = ddlRegion.Items.FindByValue(obj.MAQUOCGIA);
            if(index!=null)

                ddlRegion.SelectedIndex = ddlRegion.Items.IndexOf(index);
            ddlRegion.SelectedIndex = 0;
            //Upinfor.Update();
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            CONGTY _congty = new CONGTY();
            _congty.ID = Acount.MACT; ;
            _congty.TENCONGTY = txtCompanyName.Text.Trim();
            _congty.WEBSITE = txtwebsite.Text.Trim();
            _congty.MAQUOCGIA = ddlRegion.SelectedValue.ToString();
            _congty.MADM = ddlCategory.SelectedValue.ToString();
            _congty.KEYWORD = txtkeyword.Text.Trim();
            _congty.Email = txtMail.Text.Trim();
            _congty.MOTA = txtmota.Text.Trim();
            int kq = _congtydao.Update(_congty);
           

        }
    }
}